home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c,comp.sys.amiga.programmer
- Subject: Re: MY SORT DOESN'T QUICK!
- Date: Sat, 30 Mar 96 20:33:04 GMT
- Organization: none
- Message-ID: <828217984snz@genesis.demon.co.uk>
- References: <2082.6659T1310T2974@xs4all.nl>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <2082.6659T1310T2974@xs4all.nl>
- muaddib@xs4all.nl "Thomas van Gulick" writes:
-
- >Help! My Sort doesn't Quick (ie, it doesn't work for all inputs!)
- >Something goes wrong when the array is being split into two parts,
- >but how to solve it, beats me. (ULONG = unsigned long btw)
- >
- >The array where this function fails:
- >
- >{ 110, 1664, 96781, 1, 5, 10, 1120, 10, 130, 10 }
-
- You have a small set of data here that shows the problem. Run therough the
- code step by step with that data (probably best by hand although you
- might use a debugger) untill you see something that goes wrong. That should
- give you a clue as to what the problem is.
-
- The question is though why are you doing this? Is it a assignment or are
- you just interested in getting to know some sort algorithms? I assume that
- yoy don't simply need a sort for an application since you could use qsort()
- for that or get much better code off the net.
-
- >VOID
- >QuickSort
- >(
- > ULONG Array[],
- > ULONG lo0,
- > ULONG hi0
- >)
- >{
- > ULONG lo = lo0;
- > ULONG hi = hi0;
- >
- > if( lo < hi )
- > {
- > ULONG mid;
- >
- > mid = Array[ ( lo + hi ) / 2 ];
- >
- > while( lo < hi )
- > {
- > while( Array[ lo ] < mid )
- > lo ++;
- >
- > while( Array[ hi ] > mid )
- > hi --;
- >
- > if( lo < hi )
- > {
- > ULONG swap;
- >
- > swap = Array[ lo ];
- > Array[ lo ] = Array[ hi ];
- > Array[ hi ] = swap;
- >
- > lo ++;
- > hi --;
- > }
- > }
- > QuickSort( Array, lo0, hi );
- > QuickSort( Array, lo == hi ? lo + 1 : lo, hi0 );
- > }
- >}
-
- A proper partitioning algorithm will arrange the elements so that they
- satisfy 3 properties:
-
- 1. The 'pivot' is in its final position for the entire sort i.e. never
- needs to be moved again.
-
- 2. All elements positioned lower than the pivot compare less than or equal to
- it.
-
- 3. All elements positioned higher than the pivot compare greater than or
- equal to it.
-
- The code above doesn't appear to position the pivot element properly (what
- you are storing in mid), it just seems to try to create 2 ranges, which is
- not a correct approach for partitioning.
-
- >I need an answer or solution as soon as possible! It's of a live saving matter!
- >(Well sort of:)
-
- What do you need it for?
-
- >Oh, and don't tell me it is built into my compiler.
-
- While it doesn't necessarily implement quicksort, qsort() is part of the
- standard library.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-